﻿// dlg_net_ctrl.cpp: 实现文件
//

#include "pch.h"
#include "CamCtrl.h"
#include "afxdialogex.h"
#include "dlg_net_ctrl.h"


// dlg_net_ctrl 对话框

IMPLEMENT_DYNAMIC(dlg_net_ctrl, CDialogEx)


BEGIN_MESSAGE_MAP(dlg_net_ctrl, CDialogEx)
	ON_NOTIFY(IPN_FIELDCHANGED, IDC_LOCAL_IP_ADDR, &dlg_net_ctrl::OnIpnFieldchangedLocalIpAddr)
	ON_BN_CLICKED(IDC_EN_IPADDR_AUTO, &dlg_net_ctrl::OnBnClickedEnIpaddrAuto)
END_MESSAGE_MAP()



dlg_net_ctrl::dlg_net_ctrl(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_CAMCTRL_DIALOG, pParent)
{

}

dlg_net_ctrl::~dlg_net_ctrl()
{
}

int dlg_net_ctrl::set(struct device_info* info)
{
	((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->SetCurSel(info->net_card_type);
	((CButton*)GetDlgItem(IDC_EN_IPADDR_AUTO))->SetCheck(info->auto_ip_addr ? TRUE : FALSE);
	((CIPAddressCtrl*)GetDlgItem(IDC_LOCAL_IP_ADDR))->SetAddress(info->ip_addr[0], info->ip_addr[1], info->ip_addr[2], info->ip_addr[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_SUBNET_MASK))->SetAddress(info->sub_net_mask[0], info->sub_net_mask[1], info->sub_net_mask[2], info->sub_net_mask[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_GATEWAY))->SetAddress(info->gate_way[0], info->gate_way[1], info->gate_way[2], info->gate_way[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_MAIN_DNS))->SetAddress(info->major_dns[0], info->major_dns[1], info->major_dns[2], info->major_dns[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_ALTERNATE_DNS))->SetAddress(info->minor_dns[0], info->minor_dns[1], info->minor_dns[2], info->minor_dns[3]);

	GetDlgItem(IDC_EDIT_DEVICE_NAME)->SetWindowText(CString(info->device_name));

	return 0;
}

int dlg_net_ctrl::get(struct device_info* info)
{
	info->net_card_type = ((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->GetCurSel();
	info->auto_ip_addr  = ((CButton*)GetDlgItem(IDC_EN_IPADDR_AUTO))->GetCheck() ? TRUE : FALSE;
	((CIPAddressCtrl*)GetDlgItem(IDC_LOCAL_IP_ADDR))->GetAddress(info->ip_addr[0], info->ip_addr[1], info->ip_addr[2], info->ip_addr[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_SUBNET_MASK))->GetAddress(info->sub_net_mask[0], info->sub_net_mask[1], info->sub_net_mask[2], info->sub_net_mask[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_GATEWAY))->GetAddress(info->gate_way[0], info->gate_way[1], info->gate_way[2], info->gate_way[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_MAIN_DNS))->GetAddress(info->major_dns[0], info->major_dns[1], info->major_dns[2], info->major_dns[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_ALTERNATE_DNS))->GetAddress(info->minor_dns[0], info->minor_dns[1], info->minor_dns[2], info->minor_dns[3]);

	CString str;
	GetDlgItem(IDC_EDIT_DEVICE_NAME)->GetWindowText(str);
	memcpy(info->device_name,str.GetBuffer(),str.GetLength());

	return 0;
}

BOOL dlg_net_ctrl::valid(void)
{
	return 1;
}

void dlg_net_ctrl::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}



BOOL dlg_net_ctrl::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->InsertString(0, "有线网卡");
	((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->InsertString(1, "WIFI");
	((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->InsertString(2, "4G模块");
	((CComboBox*)GetDlgItem(IDC_NETCARD_TYPE))->SetCurSel(0);
	((CButton*)GetDlgItem(IDC_EN_IPADDR_AUTO))->SetCheck(FALSE);
	((CIPAddressCtrl*)GetDlgItem(IDC_LOCAL_IP_ADDR))->SetAddress(0,0,0,0);
	((CIPAddressCtrl*)GetDlgItem(IDC_SUBNET_MASK))->SetAddress(255, 255, 255, 0);
	((CIPAddressCtrl*)GetDlgItem(IDC_GATEWAY))->SetAddress(0, 0, 0, 0);
	((CIPAddressCtrl*)GetDlgItem(IDC_MAIN_DNS))->SetAddress(0, 0, 0, 0);
	((CIPAddressCtrl*)GetDlgItem(IDC_ALTERNATE_DNS))->SetAddress(0, 0, 0, 0);	
	GetDlgItem(IDC_EDIT_DEVICE_NAME)->SetWindowText("");

	return TRUE;  
}

void dlg_net_ctrl::OnIpnFieldchangedLocalIpAddr(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMIPADDRESS pIPAddr = reinterpret_cast<LPNMIPADDRESS>(pNMHDR);
	u8 ip[4];
	((CIPAddressCtrl*)GetDlgItem(IDC_LOCAL_IP_ADDR))->GetAddress(ip[0], ip[1], ip[2], ip[3]);
	((CIPAddressCtrl*)GetDlgItem(IDC_GATEWAY))->SetAddress(ip[0], ip[1], ip[2], 1);
	((CIPAddressCtrl*)GetDlgItem(IDC_SUBNET_MASK))->SetAddress(255, 255, 255, 0);
	*pResult = 0;
}


void dlg_net_ctrl::OnBnClickedEnIpaddrAuto()
{
	BOOL state = ((CButton*)GetDlgItem(IDC_EN_IPADDR_AUTO))->GetCheck();
	GetDlgItem(IDC_LOCAL_IP_ADDR)->EnableWindow(!state);
	GetDlgItem(IDC_SUBNET_MASK)->EnableWindow(!state);
	GetDlgItem(IDC_GATEWAY)->EnableWindow(!state);
	GetDlgItem(IDC_MAIN_DNS)->EnableWindow(!state);
	GetDlgItem(IDC_ALTERNATE_DNS)->EnableWindow(!state);
}
